﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BusinessLayer.WorldArtPrice.Entities;
using BusinessLayer.WorldArtPrice.Models;
using DBHelper;

namespace BusinessLayer.WorldArtPrice
{
    public class ContentBL
    {
        private readonly DbHelper _db;

        public ContentBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public Content GetByContentTypeId(int contentTypeId)
        {
            var oCommand = new SqlCommand("sp_Content_Select_By_ContentTypeId") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ContentTypeId", contentTypeId));

            var list = _db.GetList<Content>(oCommand);
            return list.FirstOrDefault();
        }

        public Content GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Content_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));

            var list = _db.GetList<Content>(oCommand);
            return list.FirstOrDefault();
        }

        public List<ContentModel> GetAll()
        {
            var oCommand = new SqlCommand("sp_Content_Select_All") { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<ContentModel>(oCommand);
            return list;
        }

        public void Insert(Content content)
        {
            var oCommand = new SqlCommand("sp_Content_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Contents", content.Contents));
            oCommand.Parameters.Add(new SqlParameter("@ContentTypeId", content.ContentTypeId));

            _db.ExecuteNonQuery(oCommand);            
        }

        public void Update(Content content)
        {
            var oCommand = new SqlCommand("sp_Content_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", content.Id));
            oCommand.Parameters.Add(new SqlParameter("@Contents", content.Contents));
            oCommand.Parameters.Add(new SqlParameter("@ContentTypeId", content.ContentTypeId));

            _db.ExecuteNonQuery(oCommand);
        }

        public void Delete(int id)
        {
            var oCommand = new SqlCommand("sp_Content_Delete") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));

            _db.ExecuteNonQuery(oCommand);
        }

    }
}
